package com.example.lenovo.a203_yhy_ordering_3.model;

import com.example.lenovo.a203_yhy_ordering_3.beans.FoodListBean;
import com.example.lenovo.a203_yhy_ordering_3.beans.RegisterBean;
import com.example.lenovo.a203_yhy_ordering_3.iface.FoodLstIface;
import com.example.lenovo.a203_yhy_ordering_3.listener.FoodListListener;
import com.example.lenovo.a203_yhy_ordering_3.listener.RegisterListener;
import com.example.lenovo.a203_yhy_ordering_3.service.FoodListService;
import com.example.lenovo.a203_yhy_ordering_3.service.RegisterService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by lenovo on 2018/11/29.
 */

public class FoodListModel implements FoodLstIface{
    private Retrofit retrofit;

    public FoodListModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public void getFoodListResult(int shop_id,final FoodListListener foodListListener){
        FoodListService foodListService = retrofit.create(FoodListService.class);

        Call<List<FoodListBean>> foodListCall = foodListService.getFoodByShop(shop_id);

        foodListCall.enqueue(new Callback<List<FoodListBean>>() {
            @Override
            public void onResponse(Call<List<FoodListBean>> call, Response<List<FoodListBean>> response) {
                if(response!=null)
                    foodListListener.onResponse(response.body());
                else
                    foodListListener.onFail("获取食品列表失败");
            }

            @Override
            public void onFailure(Call<List<FoodListBean>> call, Throwable t) {
                    foodListListener.onFail(t.toString());
            }
        });
    }
}
